/* 
 * File:   SysMenu.cpp
 * Author: edwin
 *
 * 
 */
#include "SysMenu.h"

SysMenu::SysMenu()
{
	//
	package_system.readPackageData();
	newBooking.readBookingData();
	newBooking.readDestinationData();
	newBooking.readPackageData();
}

SysMenu::~SysMenu()
{
	// destructor
}

string SysMenu::numToString(int num)
{
	string Result;          // string which will contain the result
	ostringstream convert;   // stream used for the conversion
	convert << num;      // insert the textual representation of 'Number' in the characters in the stream
	Result = convert.str(); // set 'Result' to the contents of the stream
	return Result;
}

int SysMenu::stringToNum(string str)
{
	return atoi( str.c_str() );
}

void SysMenu::run()
{

	User authenticatedUser;
	authenticatedUser = newSession.run();

	user_access = atoi((authenticatedUser.getUserRole()).c_str());
	//User newUser = ;

	bool usrQuit = false;
	char choice;
	do {
		//cout << "Please choose an option" << endl;
		// Check if user is a client
		if (user_access == 5) {
			BookingManagement(authenticatedUser);
			cout << "Client session ended" << endl;
			usrQuit = true;
		} else {
			cout << "***********************************************" << endl;
			cout << "        SmartTravel Booking System             " << endl;
			cout << "***********************************************" << endl;
			cout << "1. Holiday Packages Management" << endl;
			cout << "2. Staff Account Management" << endl;
			cout << "3. Client Account Management" << endl;
			cout << "4. Booking Management" << endl;

			cout << "Option: ";
			cin >> choice;
			cin.ignore();

			switch (choice) {
				case '0':
					cout << "Super secret admin option" << endl;
					break;
				case '1':
					PackageManagement(user_access);
					break;
				case '2':
					StaffManagement(user_access);
					break;
				case '3':
					ClientManagement(user_access);
					break;
				case '4':
					BookingManagement(authenticatedUser);
					break;
				default:
					cout << "Unknown unpdf" << endl;
					usrQuit = true;
					break;
			}


		}


	} while (!usrQuit);
}

void SysMenu::PackageManagement(int role)
{
	if (role != 1) {
		cout << "You do not have access permissions" << endl;
	} else {
		char usrOption;

		cout << "***********************************************" << endl;
		cout << "        Holiday Package Management             " << endl;
		cout << "***********************************************" << endl;
		cout << "1. Add New Holiday Package" << endl;
		cout << "2. Edit Existing Holiday Package" << endl;
		cout << "3. Delete Exisitng Holiday Package" << endl;
		cout << "Please choose an option: ";
		cin >> usrOption;
		cin.ignore();

		switch (usrOption) {
			case '1':
				package_system.addNewPackage();
				break;
			case '2':
				package_system.editPackage();
				break;
			case '3':
				package_system.deletePackage();
				break;
			default:
				cout << "unknown: Holiday Management" << endl;
				//usrQuit = true;
				break;
		}
	}

	//package_system.showPackage();
	package_system.writePackageData();
}

void SysMenu::StaffManagement(int role)
{
	if (role != 1) {
		cout << "You do not have access permissions" << endl;
	} else {
		char usrOption;

		cout << "***********************************************" << endl;
		cout << "               Staff Management                " << endl;
		cout << "***********************************************" << endl;
		cout << "1. Add New Staff Account" << endl;
		cout << "2. Edit Existing Staff Account" << endl;
		cout << "3. Delete Existing Staff Account" << endl;
		cout << "-----> Please enter an option: ";
		cin >> usrOption;
		cin.ignore();

		switch (usrOption) {
			case '1':
				newSession.addNewUser();
				break;
			case '2':
				newSession.editExistingUser();
				break;
			case '3':
				newSession.deleteExistingUser();
				break;
			default:
				cout << "unknown: Staff Management" << endl;
				//usrQuit = true;
				break;
		}
	}
	newSession.writeUserData();
	newSession.showUserData();

}

void SysMenu::ClientManagement(int role)
{

	if (role == 2 || role == 3) {
		cout << "***********************************************" << endl;
		cout << "               Client Management               " << endl;
		cout << "***********************************************" << endl;

		// Booking Staff
		if ( role == 3 ) {
			char usrOption;

			cout << "1. Add New Client Account" << endl;
			cout << "2. Edit Existing Client Account" << endl;
			cout << "Enter your option: ";
			cin >> usrOption;
			cin.ignore();

			if (usrOption == 1) {
				newSession.addNewClient();
			} else if (usrOption == 2) {
				newSession.editExistingClient();
			}
		// Holiday Manager
		} else if (role == 2) {
			char usrOption;

			cout << "1. Delete Existing Client Account" << endl;
			cout << "Enter your option: ";
			cin >> usrOption;
			cin.ignore();

			if(usrOption == 1) {
				newSession.deleteExistingClient();
			}
		}
	} else {
		cout << "You do not have enough permission to access this function" << endl;
	}
}

int SysMenu::roleCheck()
{
	return 0;
}

void SysMenu::BookingManagement(User &authUser)
{
	char usrOption;

	bool returnMenu = false;

	while(!returnMenu) {

		
		//cout << newBooking.getNewRefNumber() << endl;



		// User is a customer
		if(stringToNum(authUser.getUserRole()) == 5) {
			cout << "***********************************************" << endl;
			cout << "             SmartTravel Bookings              " << endl;
			cout << "***********************************************" << endl;

			cout << "1. Make a Booking" << endl;
			cout << "2. Edit an Existing Booking" << endl;
			cout << "3. Cancel a Booking" << endl;
			cout << "4. Return to previous menu" << endl;

			cout << "-----> Please enter an option: ";
			
			cin >> usrOption;
			cin.ignore();

			switch (usrOption) {
				case '1':
					newBooking.createBooking();
					newBooking.writeBookingData();
					break;
				case '2':
					newBooking.editBooking(authUser.getUserName(), authUser.getUserRole());
					newBooking.writeBookingData();
					break;
				case '3':
					newBooking.cancelBooking(authUser.getUserName(), authUser.getUserRole());
					newBooking.writeBookingData();
					break;
				case '4':
					returnMenu = true;
					break;
				default:
					cout << "SmartTravel Bookings: ***** Invalid option selected. Please try again *****" << endl;
					break;
			}
		} else {
			cout << "***********************************************" << endl;
			cout << "     SmartTravel Bookings Management           " << endl;
			cout << "***********************************************" << endl;

			// Booking Staff
			if (stringToNum(authUser.getUserRole()) == 2) {
				cout << "Checking for Wait Listed or Payment Required forms ..." << endl;
				newBooking.vetBooking();
			} else if (stringToNum(authUser.getUserRole()) == 3) {
				cout << "Checking for Incomplete / Invalid forms ..." << endl;
				newBooking.checkBooking();
			} else if (stringToNum(authUser.getUserRole()) == 4) {
				cout << "Checking for Cancel or Confirmed forms ..." << endl;
				newBooking.confirmBooking();
			} else {
				cout << "Unknow User" << endl;
				returnMenu = true;
			}
		}
	}


}

